package com.yiren.backstage.communication.services;

import com.yiren.backstage.communication.interfaces.DataCallback;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Time:2021/1/21 14:45
 * Author:xiaozan
 * Description:设备通讯 父类
 */
public abstract class AbstractPortService {

    public InputStream inputStream;
    public OutputStream outputStream;
    public DataCallback dataCallback;

    private byte[] data;
    private int index1;
    private int index2;

    public AbstractPortService() {
        this.data = new byte[512];
        index1 = index2 = 0;
    }

    public void setDataCallback(DataCallback dataCallback) {
        this.dataCallback = dataCallback;
    }

    public DataCallback getDataCallback() {
        return dataCallback;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    /**
     * 设置参数
     *
     * @param key
     * @param obj
     */
    public void setParam(String key, Object obj) {

    }

    /**
     * 启动通讯
     *
     * @return
     */
    public boolean start() throws IOException {
        return false;
    }

    /**
     * 关闭通讯
     *
     * @throws Exception
     */
    public void stop() throws Exception {
        if (inputStream != null) {
            inputStream.close();
            inputStream = null;
        }
        if (outputStream != null) {
            outputStream.close();
            outputStream = null;
        }
    }

    /**
     * 发送数据
     *
     * @param data
     * @throws IOException
     */
    public void write(byte[] data) throws IOException {
        outputStream.write(data);
        outputStream.flush();
    }

    public void onReadData(String line) {
        if (null != dataCallback) dataCallback.onReadData(line);
    }

    /**
     * 读取数据
     *
     * @param data
     * @param offset
     * @param size
     * @return
     * @throws IOException
     */
    public String read(byte[] data, int offset, int size) throws IOException {
        //return inputStream.read(data, offset, size);
        String readLine = null;
        size = inputStream.read(data, 0, size);
        if (size > 0) {
            if (index2 + size > data.length) {
                index1 = index2 = 0;
            }
            System.arraycopy(data, offset, this.data, index2, size);
            index2 += size;

            for (int i = index1; i < index2 - 1; i++) {
                if (this.data[i] == 0x0D && this.data[i + 1] == 0x0A) {
                    readLine = new String(this.data, index1, i - index1);
                    if (null != dataCallback) dataCallback.onReadData(readLine);
                    index1 = i + 2;
                    i++;
                }
            }

            if (index1 < index2) {
                System.arraycopy(this.data, index1, this.data, 0, index2 - index1);
                index2 -= index1;
                index1 = 0;
            } else {
                index1 = index2 = 0;
            }
        }
        return readLine;
    }

}
